package com.llc.gulimall.order.web;

import com.llc.common.to.mq.OrderTo;
import com.llc.common.vo.MemberResponseVo;
import com.llc.gulimall.order.entity.OrderEntity;
import com.llc.gulimall.order.service.OrderService;
import com.llc.gulimall.order.vo.OrderConfirmVo;
import com.llc.gulimall.order.vo.SubmitOrderResponseVo;
import com.llc.gulimall.order.vo.OrderSubmitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.ExecutionException;

/**
 * @author wangwei
 * 2021/1/18 13:50
 */
@Slf4j
@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("/test/createOrder")
    @ResponseBody
    public String createOrderTest(){
        //订单下单成功
        OrderEntity entity = new OrderEntity();
        entity.setOrderSn(UUID.randomUUID().toString());
        //给mq发消息
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        OrderTo orderTo = new OrderTo();
        BeanUtils.copyProperties(orderEntity,orderTo);
        rabbitTemplate.convertAndSend("order-event-exchange","order.release.other",orderTo);
        System.out.println("test发送");
        //rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",entity);
        return "ok";

    }


    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();
    //        额外添加的
    //        HttpSession session = request.getSession();
    //        MemberResponseVo attribute = (MemberResponseVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
    //        loginUser.set(attribute);

    //@GetMapping("/toTrade")
    //public String listpage(HttpServletResponse response,HttpServletRequest request,Model model) throws IOException {
    //    HttpSession session = request.getSession();
    //    MemberResponseVo attribute = (MemberResponseVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
    //    loginUser.set(attribute);
    //    System.out.println("attribute:"+attribute);
    //    System.out.println("到达这个跳转的页面了");
    //    return "confirm";
    //}


    /**
     * 用户订单列表
     * @return
     */
    //@GetMapping("/center/list.html")
    //public String orderList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") String pageNum,
    //                        @RequestParam(value = "pageSize", required = false, defaultValue = "4") String pageSize,
    //                        Model model) {
    //    Map<String, Object> params = new HashMap<>();
    //    params.put(Constant.PAGE, pageNum);
    //    params.put(Constant.LIMIT, pageSize);
    //    PageUtils page = orderService.getCurrentUserOrderList(params);
    //    model.addAttribute("page", page);
    //    return "list";
    //}
    @GetMapping("/{page}.html")
    public String pageList(@PathVariable("page") String page){
        System.out.println("跳转到:"+page+"页面");
        return page;
    }

    /**
     * 用户从购物车页面点击去结算
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        // 得到用户本次结算时购物车中选中的那些购物项

        OrderConfirmVo orderConfirmVO = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", orderConfirmVO);
        System.out.println("orderConfirmData"+orderConfirmVO);
        return "confirm";
    }

    /**
     * 提交订单，创建订单
     *
     */
    @PostMapping("/submitOrder")
    public String submit(OrderSubmitVo submitVo, Model model) {
        //System.out.println("订单提交数据"+submitVo);
        //return null;
        log.info("准备提交");
        SubmitOrderResponseVo responseVo = orderService.submitOrder(submitVo);
        log.info("执行好提交");
        System.out.println("获得的代码："+responseVo.getCode());
        if(responseVo.getCode() == 0){
            //去支付页
            model.addAttribute("submitOrderResp",responseVo);
            log.info("准备跳转");
            return "pay";
        }
        else {
            log.info("到这了");
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }

    //@GetMapping("/topay")
    //public String topay(@RequestParam("orderSn") String orderSn,
    //                    Model model) {
    //    OrderCreateVo resp = orderService.getOrderDetail(orderSn);
    //    model.addAttribute("orderCreateVO", resp);
    //    return "pay";
    //}
    //
    ///**
    // * 以/api开始的为远程调用，需要被拦截器直接放行
    // * @param orderSn
    // * @return
    // */
    //@ResponseBody
    //@GetMapping("/api/info/{orderSn}")
    //public R getOrderDetail(@PathVariable("orderSn") String orderSn) {
    //    OrderTO resp = orderService.getOrderTOByOrderSn(orderSn);
    //    return R.ok().setData(resp);
    //}
}
